######################################################################################
##Replication Code for ###############################################################
##Decision by Design: Leaders, Bureaucracies, and International Crisis Performance ###
##Tyler Jost #########################################################################
##International Studies Quarterly ####################################################
##(1) Dataframe Build ################################################################
######################################################################################

############################################
##Load NSI Data ############################
############################################

rm(list = ls())
require(countrycode)
require(readstata13)
require(tidyr)
require(dplyr)
require(here)
load("nsi_dataset_v1.RData")
d2 <- read.csv("crisis_dyad.csv", header = T, stringsAsFactors = F)
d3 <- read.csv("crisis_outcomes.csv")

############################################
##Build Dyadic Crisis Dataframe ############
############################################

##Subset to Trigger Year of Each Crisis-Dyad
d2 <- d2 %>%
  group_by(crisno, statea, stateb) %>%
  arrange(year) %>%
  slice(1) %>%
  ungroup()

##Merge Outcome for Country A to Dyadic Dataframe
d2 <- merge(d2, d3, by.x=c("crisno", "statea"), by.y=c("crisno", "cracid"), all.x=T)

##Merge Outcome for Country B to Dyadic Dataframe
d2 <- merge(d2, d3, by.x=c("crisno", "stateb"), by.y=c("crisno", "cracid"), suffixes=c("A", "B"), all.x=T)

##Create Measures for Crisis and Crisis Failure
d2$crisis_initA <- NA
d2$crisis_initA <- ifelse(d2$first_moverA==1,1,0)
d2$crisis_initB <- NA
d2$crisis_initB <- ifelse(d2$first_moverB==1,1,0)
d2$crisis_failA <- NA
d2$crisis_failA <- ifelse((d2$outcomA==3 | d2$outcomA==4),1,0)
d2$crisis_failB <- NA
d2$crisis_failB <- ifelse((d2$outcomB==3 | d2$outcomB==4),1,0)
d2$crisis_init_failA <- NA
d2$crisis_init_failA <- ifelse((d2$first_moverA==1 & (d2$outcomA==3 | d2$outcomA==4)),1,0)
d2$crisis_init_failB <- NA
d2$crisis_init_failB <- ifelse(d2$first_moverB==1 & (d2$outcomB==3 | d2$outcomB==4),1,0)
d2$crisis_successA <- NA
d2$crisis_successA <- ifelse((d2$outcomA==1 | d2$outcomA==2),1,0)
d2$crisis_successB <- NA
d2$crisis_successB <- ifelse((d2$outcomB==1 | d2$outcomB==2),1,0)
d2$crisis_init_successA <- NA
d2$crisis_init_successA <- ifelse((d2$first_moverA==1 & (d2$outcomA==1 | d2$outcomA==2)),1,0)
d2$crisis_init_successB <- NA
d2$crisis_init_successB <- ifelse((d2$first_moverB==1 & (d2$outcomB==1 | d2$outcomB==2)),1,0)

##Create A Holder Dataframe for State A Codings
var <- c("crisno", "year", "statea", "stateb", "crisis_initA", "crisis_initB", "crisis_successA", "crisis_successB",
         "crisis_failA", "crisis_failB", "crisis_init_failA", "crisis_init_failB", "crisis_init_successA", "crisis_init_successB", "outcomA", "outcomB")
holder1 <- subset(d2, select=var)

##Create A Second Holder Dataframe for State B Codings
holder2 <- holder1
colnames(holder2) <- c("crisno", "year", "stateb", "statea", "crisis_initB", "crisis_initA", "crisis_successB", "crisis_successA",
                    "crisis_failB", "crisis_failA", "crisis_init_failB", "crisis_init_failA", "crisis_init_successB", "crisis_init_successA", "outcomB", "outcomA")

##Merge Holder Dataframes
master <- rbind(holder1, holder2)
master <- master[order(master$crisno, master$year, master$statea, master$stateb),]

##Manually Fix Erroneous Country Code for Yemen
master$statea[master$statea==678 & master$year>=1993 & is.na(master$stateb)==F] <- 679
master$stateb[master$stateb==678 & master$year>=1993 & is.na(master$stateb)==F] <- 679

##Limit to Study Period (1946 to 2012)
master <- master[master$year>=1946 & master$year<=2012 & is.na(master$year)==F,]

############################################
##Merge Dataframe with NSI Data ############
############################################

d <- merge(master, d, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x = T, all.y = F)

############################################
##Merge Covariate Data #####################
############################################

##National Military Capabilities
nmc <- read.dta13("NMC_5_0.dta")
var <- c("ccode", "year", "cinc")
nmc <- subset(nmc, select = var)
d <- merge(d, nmc, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d <- merge(d, nmc, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))
d$cinc_rel <- d$cincA / (d$cincA + d$cincB)
d$LcincA <- log(d$cincA+1)
d$LcincB <- log(d$cincB+1)
d$Lcinc_rel <- d$LcincA / (d$LcincA + d$LcincB)

##ATOP Alliances S Scores
alliance <- read.csv("atop_sscores.csv", header=T, stringsAsFactors=F)
alliance.ab <- alliance[alliance$year>=1946,]
var <- c("ccode1", "ccode2", "year", "s_un_atop", "s_wt_atop")
alliance.ab <- subset(alliance.ab, select=var)
alliance.ba <- alliance.ab
colnames(alliance.ba) <- c("ccode2", "ccode1", "year", "s_un_atop", "s_wt_atop")
alliance <- rbind(alliance.ab, alliance.ba)
d <- merge(d, alliance, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GDP
gdp <- read.delim("gdpv6.txt", header = TRUE, sep = "\t", dec = ".")
gdp$Lrealgdp <- log(gdp$realgdp+1)
var <- c("statenum", "year", "Lrealgdp")
gdp <- subset(gdp, select=var)
d <- merge(d, gdp, by.x=c("statea", "year"), by.y=c("statenum", "year"), all.x=T)
d <- merge(d, gdp, by.x=c("stateb", "year"), by.y=c("statenum", "year"), all.x=T, suffixes = c("A", "B"))

##Trade
trade <- read.csv("dyad_trade_cow_v4.csv", header=T, stringsAsFactors=F, na.strings = -9)
trade$trade <- trade$flow1 + trade$flow2
trade$Ltrade <- log(trade$trade+1)
var <- c("ccode1", "ccode2", "year", "Ltrade")
trade.ab <- subset(trade, select=var)
trade.ba <- trade.ab
colnames(trade.ab) <- c("ccode2", "ccode1", "year", "Ltrade")
trade <- rbind(trade.ab, trade.ba)
d <- merge(d, trade, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GWF Regime Type
gwf <- read.dta13("GWF_AllPoliticalRegimes.dta")
##Standardize Country Codes
##Germany
gwf$cowcode[gwf$cowcode==255 & gwf$year>=1955 & gwf$year <= 1990] <- 260
##Yemen
gwf$cowcode[gwf$cowcode==678 & gwf$year >= 1990] <- 679

var <- c("cowcode", "year", "gwf_military", "gwf_personal")
gwf <- subset(gwf, select=var)
d <- merge(d, gwf, by.x=c("statea", "year"), by.y=c("cowcode", "year"), all.x = T)

##Manually Code Missing Values (See Discussion in Appendix)
##Yugoslavia
d$gwf_personal[d$statea==345 & d$year==1991] <- 1
d$gwf_military[d$statea==345 & d$year==1991] <- 0
##Bosnia
d$gwf_personal[d$statea==346 & d$year==2010] <- 0
d$gwf_military[d$statea==346 & d$year==1991] <- 0
d$gwf_military[d$statea==346 & d$year==2010] <- 0
##India
d$gwf_military[d$statea==750 & d$year==1947] <- 0
d$gwf_personal[d$statea==750 & d$year==1947] <- 0
##Pakistan
d$gwf_military[d$statea==770 & d$year==1947] <- 0
d$gwf_personal[d$statea==770 & d$year==1947] <- 0
##Israel
d$gwf_military[d$statea==666 & d$year==1948] <- 0
d$gwf_personal[d$statea==666 & d$year==1948] <- 0
##Vietnam
d$gwf_military[d$statea==816 & d$year==1954] <- 0
d$gwf_personal[d$statea==816 & d$year==1954] <- 0

##Merge Leaders Data
leaders <- read.csv("leaders_updated.csv", stringsAsFactors = F, header = T)
leaders <- subset(leaders, select=c("leader", "ccode", "year", "yrborn", "gender", "milservice", "combat", "hos_experience"))
leaders <- leaders[!duplicated(leaders),]
d <- merge(d, leaders, by.x = c("statea", "year", "hos_name_arc"), by.y = c("ccode", "year", "leader"), all.x = T, all.y = F)

##Create Variable for Leader Age
d$age <- d$year - d$yrborn

##Create a Variable for the Cold War
d$cw <- 1
d$cw[d$year>=1991] <- 0

##Polity V
pol <- read.csv("p5v2018.csv", stringsAsFactors = F, header = T)
var <- c("ccode", "year", "democ", "autoc", "polity2", "regtrans")
pol <- subset(pol, select = var)

##Standardize Polity Country Codes
##Soviet Union
pol$ccode[pol$ccode==364] <- 365
##Ethiopia
pol$ccode[pol$ccode==529] <- 530
##Pakistan
pol$ccode[pol$ccode==769] <- 770
##Vietnam
pol$ccode[pol$ccode==818] <- 816
##Yugoslavia/Serbia
pol$ccode[pol$ccode==347] <- 345
pol$ccode[pol$ccode==342] <- 345
##Drop Duplicates
pol <- pol %>% distinct(ccode, year, .keep_all = TRUE)

##Code Transition Cases (See Discussion in Appendix)
##East Germany
pol$polity2[pol$ccode==265 & pol$year>=1989 & pol$year<=1990] <- (-9+-9)/2
##Hungary
pol$polity2[pol$ccode==310 & pol$year==1956] <- (-7+-7)/2
##Czechoslovakia
pol$polity2[pol$ccode==315 & pol$year==1968] <- (-7+-7)/2
##Bosnia
pol$polity2[pol$ccode==346 & pol$year>=1995 & pol$year<=2010] <- 0
##Uganda
pol$polity2[pol$ccode==500 & pol$year==1979] <- -7
#Iraq
pol$polity2[pol$ccode==645 & pol$year>=2003 & pol$year<=2009] <- (-9+3)/2
##Syria
pol$polity2[pol$ccode==652 & pol$year>=1958 & pol$year<=1961] <- (7+2)/2
##Lebanon
pol$polity2[pol$ccode==660 & pol$year>=1990 & pol$year<=2004] <- (0+6)/2
##Kuwait
pol$polity2[pol$ccode==690 & pol$year==1990] <- (-10+-9)/2
##Afghanistan
pol$polity2[pol$ccode==700 & pol$year>=1979 & pol$year<=1988] <- (0+-8)/2
pol$polity2[pol$ccode==700 & pol$year>=2002 & pol$year<=2010] <- (-7+-1)/2
##India
pol$polity2[pol$ccode==750 & pol$year>=1947 & pol$year<=1951] <- 5
##Cambodia
pol$polity2[pol$ccode==811 & pol$year>=1979 & pol$year<=1989] <- -7
##South Vietnam
pol$polity2[pol$ccode==817 & pol$year>=1965 & pol$year<=1972] <- (-3+0)/2

d <- merge(d, pol, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d <- merge(d, pol, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))

##Code Missing Values (See Discussion in Appendix)
##Kosovo
d$polity2A[d$statea==341 & d$year>=2006 & d$year<=2007] <- 8
d$polity2B[d$stateb==341 & d$year>=2006 & d$year<=2007] <- 8
#Ghana
d$polity2A[d$statea==452 & d$year>=1957 & d$year<=1959] <- 1
d$polity2B[d$stateb==452 & d$year>=1957 & d$year<=1959] <- 1
##Zimbabwe/Rhodesia
d$polity2A[d$statea==552 & d$year>=1965 & d$year<=1979] <- 4
d$polity2B[d$stateb==552 & d$year>=1965 & d$year<=1979] <- 4
##Egypt
d$polity2A[d$statea==651 & d$year>=1946 & d$year<=1950] <- (1+5)/2
d$polity2B[d$stateb==651 & d$year>=1946 & d$year<=1950] <- (1+5)/2
##Kuwait
d$polity2A[d$statea==690 & d$year>=1961 & d$year<=1962] <- -8
d$polity2B[d$stateb==690 & d$year>=1961 & d$year<=1962] <- -8
##Afghanistan
d$polity2A[d$statea==700 & d$year==2001] <- -7
d$polity2B[d$stateb==700 & d$year==2001] <- -7
##Bangladesh
d$polity2A[d$statea==771 & d$year==1971] <- 8
d$polity2B[d$stateb==771 & d$year==1971] <- 8
##South Vietnam
d$polity2A[d$statea==817 & d$year==1954] <- -3
d$polity2B[d$stateb==817 & d$year==1954] <- -3

##Identify Regime Transitions
d$regtransA[is.na(d$regtransA)==T] <- 0
d$regtransA[d$regtransA==-77] <- 1
d$regtransA[d$regtransA==-66] <- 1
d$regtransA[d$regtransA==-2] <- 1
d$regtransA[d$regtransA==-1] <- 0
d$regtransA[d$regtransA==1] <- 0
d$regtransA[d$regtransA==2] <- 0
d$regtransA[d$regtransA==3] <- 1
d$regtransA[d$regtransA==96] <- 1
d$regtransA[d$regtransA==97] <- 1
d$regtransA[d$regtransA==98] <- 1
d$regtransA[d$regtransA==99] <- 1

##Create Variables for Major Power Status
major <- read.csv("majors2016.csv", header=T, stringsAsFactors=F)
major

##State A Major Power Status
d$majora <- 0
d$majora[d$statea %in% c(2, 200, 220, 365, 710)] <- 1
d$majora[d$year >=1991 & d$statea %in% c(255, 740)] <- 1

##State B Major Power Status
d$majorb <- 0
d$majorb[d$stateb %in% c(2, 200, 220, 365, 710)] <- 1
d$majorb[d$year >=1991 & d$stateb %in% c(255, 740)] <- 1

##Order Dataframe
d <- d[order(d$year, d$statea, d$stateb),]

save(d, file="analysis.RData")

################################################
##Build a Dataframe with Original ICB Values ###
################################################

rm(list = ls())
load("nsi_dataset_v1.RData")
d2.original <- read.csv("icbdy_v15.csv")
d3.original <- read.csv("icb2v12.csv")
d3 <- read.csv("crisis_outcomes.csv")
d3 <- subset(d3, select=c("crisno", "cracid", "first_mover"))

##Subset to Trigger Year of Each Crisis-Dyad
d2.original <- d2.original %>%
  filter(year <= 2012 & year >=1946) %>% 
  group_by(crisno, statea, stateb) %>%
  arrange(year) %>%
  slice(1) %>%
  ungroup()

##Merge Outcome for Country A to Dyadic Dataframe
d2.original <- merge(d2.original, d3.original, by.x=c("crisno", "crisname", "statea"), by.y=c("crisno", "crisname", "cracid"), all.x=T)

##Merge Outcome for Country B to Dyadic Dataframe
d2.original <- merge(d2.original, d3.original, by.x=c("crisno", "crisname", "stateb"), by.y=c("crisno", "crisname","cracid"), suffixes=c("A", "B"), all.x=T)

##Merge First-Mover Status
d2.original <- merge(d2.original, d3, by.x=c("crisno", "statea"), by.y=c("crisno", "cracid"), all.x=T, suffixes="A")
d2.original <- merge(d2.original, d3, by.x=c("crisno", "stateb"), by.y=c("crisno", "cracid"), all.x=T, suffixes=c("A", "B"))

##Create Measures for Crisis and Crisis Failure
d2.original$crisis_initA <- NA
d2.original$crisis_initA <- ifelse(d2.original$first_moverA==1,1,0)
d2.original$crisis_initB <- NA
d2.original$crisis_initB <- ifelse(d2.original$first_moverB==1,1,0)
d2.original$crisis_failA <- NA
d2.original$crisis_failA <- ifelse(d2.original$outcomA==3 | d2.original$outcomA==4,1,0)
d2.original$crisis_failB <- NA
d2.original$crisis_failB <- ifelse(d2.original$outcomB==3 | d2.original$outcomB==4,1,0)
d2.original$crisis_init_failA <- NA
d2.original$crisis_init_failA <- ifelse((d2.original$first_moverA==1 & (d2.original$outcomA==3 | d2.original$outcomA==4)),1,0)
d2.original$crisis_init_failB <- NA
d2.original$crisis_init_failB <- ifelse((d2.original$first_moverB==1 & (d2.original$outcomB==3 | d2.original$outcomB==4)),1,0)
d2.original$crisis_successA <- NA
d2.original$crisis_successA <- ifelse(d2.original$outcomA==1 | d2.original$outcomA==2,1,0)
d2.original$crisis_successB <- NA
d2.original$crisis_successB <- ifelse(d2.original$outcomB==1 | d2.original$outcomB==2,1,0)
d2.original$crisis_init_successA <- NA
d2.original$crisis_init_successA <- ifelse(d2.original$first_moverA==1 & (d2.original$outcomA==1 | d2.original$outcomA==2),1,0)
d2.original$crisis_init_successB <- NA
d2.original$crisis_init_successB <- ifelse(d2.original$first_moverB==1 & (d2.original$outcomB==1 | d2.original$outcomB==2),1,0)

##Create A Holder Dataframe for State A Codings
var <- c("crisno", "year", "statea", "stateb", "crisis_initA", "crisis_initB", "crisis_successA", "crisis_successB",
         "crisis_failA", "crisis_failB", "crisis_init_failA", "crisis_init_failB", "crisis_init_successA", "crisis_init_successB", "outcomA", "outcomB")
holder1 <- subset(d2.original, select=var)

##Create A Second Holder Dataframe for State B Codings
holder2 <- holder1
colnames(holder2) <- c("crisno", "year", "stateb", "statea", "crisis_initB", "crisis_initA", "crisis_successB", "crisis_successA",
                       "crisis_failB", "crisis_failA", "crisis_init_failB", "crisis_init_failA", "crisis_init_successB", "crisis_init_successA", "outcomB", "outcomA")

##Merge Holder Dataframes
master.original <- rbind(holder1, holder2)
master.original <- master.original[order(master.original$crisno, master.original$year, master.original$statea, master.original$stateb),]

##Manually Fix Erroneous Country Code for Yemen
master.original$statea[master.original$statea==678 & master.original$year>=1993 & is.na(master.original$stateb)==F] <- 679
master.original$stateb[master.original$stateb==678 & master.original$year>=1993 & is.na(master.original$stateb)==F] <- 679

##Limit to Study Period (1946 to 2012)
master.original <- master.original[master.original$year>=1946 & master.original$year<=2012 & is.na(master.original$year)==F,]

############################################
##Merge Dataframe with NSI Data ############
############################################

d.original <- merge(master.original, d, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x = T, all.y = F)

############################################
##Merge Covariate Data #####################
############################################

##National Military Capabilities
nmc <- read.dta13("NMC_5_0.dta")
var <- c("ccode", "year", "cinc")
nmc <- subset(nmc, select = var)
d.original <- merge(d.original, nmc, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d.original <- merge(d.original, nmc, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))
d.original$cinc_rel <- d.original$cincA / (d.original$cincA + d.original$cincB)
d.original$LcincA <- log(d.original$cincA+1)
d.original$LcincB <- log(d.original$cincB+1)
d.original$Lcinc_rel <- d.original$LcincA / (d.original$LcincA + d.original$LcincB)

##ATOP Alliances S Scores
alliance <- read.csv("atop_sscores.csv", header=T, stringsAsFactors=F)
alliance.ab <- alliance[alliance$year>=1946,]
var <- c("ccode1", "ccode2", "year", "s_un_atop", "s_wt_atop")
alliance.ab <- subset(alliance.ab, select=var)
alliance.ba <- alliance.ab
colnames(alliance.ba) <- c("ccode2", "ccode1", "year", "s_un_atop", "s_wt_atop")
alliance <- rbind(alliance.ab, alliance.ba)
d.original <- merge(d.original, alliance, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GDP
gdp <- read.delim("gdpv6.txt", header = TRUE, sep = "\t", dec = ".")
gdp$Lrealgdp <- log(gdp$realgdp+1)
var <- c("statenum", "year", "Lrealgdp")
gdp <- subset(gdp, select=var)
d.original <- merge(d.original, gdp, by.x=c("statea", "year"), by.y=c("statenum", "year"), all.x=T)
d.original <- merge(d.original, gdp, by.x=c("stateb", "year"), by.y=c("statenum", "year"), all.x=T, suffixes = c("A", "B"))

##Trade
trade <- read.csv("dyad_trade_cow_v4.csv", header=T, stringsAsFactors=F, na.strings = -9)
trade$trade <- trade$flow1 + trade$flow2
trade$Ltrade <- log(trade$trade+1)
var <- c("ccode1", "ccode2", "year", "Ltrade")
trade.ab <- subset(trade, select=var)
trade.ba <- trade.ab
colnames(trade.ab) <- c("ccode2", "ccode1", "year", "Ltrade")
trade <- rbind(trade.ab, trade.ba)
d.original <- merge(d.original, trade, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GWF Regime Type
gwf <- read.dta13("GWF_AllPoliticalRegimes.dta")
##Standardize Country Codes
##Germany
gwf$cowcode[gwf$cowcode==255 & gwf$year>=1955 & gwf$year <= 1990] <- 260
##Yemen
gwf$cowcode[gwf$cowcode==678 & gwf$year >= 1990] <- 679

var <- c("cowcode", "year", "gwf_military", "gwf_personal")
gwf <- subset(gwf, select=var)
d.original <- merge(d.original, gwf, by.x=c("statea", "year"), by.y=c("cowcode", "year"), all.x = T)

##Manually Code Missing Values (See Discussion in Appendix)
##Yugoslavia
d.original$gwf_personal[d.original$statea==345 & d.original$year==1991] <- 1
d.original$gwf_military[d.original$statea==345 & d.original$year==1991] <- 0
##Bosnia
d.original$gwf_personal[d.original$statea==346 & d.original$year==2010] <- 0
d.original$gwf_military[d.original$statea==346 & d.original$year==1991] <- 0
d.original$gwf_military[d.original$statea==346 & d.original$year==2010] <- 0
##India
d.original$gwf_military[d.original$statea==750 & d.original$year==1947] <- 0
d.original$gwf_personal[d.original$statea==750 & d.original$year==1947] <- 0
##Pakistan
d.original$gwf_military[d.original$statea==770 & d.original$year==1947] <- 0
d.original$gwf_personal[d.original$statea==770 & d.original$year==1947] <- 0
##Israel
d.original$gwf_military[d.original$statea==666 & d.original$year==1948] <- 0
d.original$gwf_personal[d.original$statea==666 & d.original$year==1948] <- 0
##Vietnam
d.original$gwf_military[d.original$statea==816 & d.original$year==1954] <- 0
d.original$gwf_personal[d.original$statea==816 & d.original$year==1954] <- 0

##Merge Leaders Data
leaders <- read.csv("leaders_updated.csv", stringsAsFactors = F, header = T)
leaders <- subset(leaders, select=c("leader", "ccode", "year", "yrborn", "gender", "milservice", "combat", "hos_experience"))
leaders <- leaders[!duplicated(leaders),]
d.original <- merge(d.original, leaders, by.x = c("statea", "year", "hos_name_arc"), by.y = c("ccode", "year", "leader"), all.x = T, all.y = F)

##Create Variable for Leader Age
d.original$age <- d.original$year - d.original$yrborn

##Create a Variable for the Cold War
d.original$cw <- 1
d.original$cw[d.original$year>=1991] <- 0

##Polity V
pol <- read.csv("p5v2018.csv", stringsAsFactors = F, header = T)
var <- c("ccode", "year", "democ", "autoc", "polity2", "regtrans")
pol <- subset(pol, select = var)

##Standardize Polity Country Codes
##Soviet Union
pol$ccode[pol$ccode==364] <- 365
##Ethiopia
pol$ccode[pol$ccode==529] <- 530
##Pakistan
pol$ccode[pol$ccode==769] <- 770
##Vietnam
pol$ccode[pol$ccode==818] <- 816
##Yugoslavia/Serbia
pol$ccode[pol$ccode==347] <- 345
pol$ccode[pol$ccode==342] <- 345
##Drop Duplicates
pol <- pol %>% distinct(ccode, year, .keep_all = TRUE)

##Code Transition Cases (See Discussion in Appendix)
##East Germany
pol$polity2[pol$ccode==265 & pol$year>=1989 & pol$year<=1990] <- (-9+-9)/2
##Hungary
pol$polity2[pol$ccode==310 & pol$year==1956] <- (-7+-7)/2
##Czechoslovakia
pol$polity2[pol$ccode==315 & pol$year==1968] <- (-7+-7)/2
##Bosnia
pol$polity2[pol$ccode==346 & pol$year>=1995 & pol$year<=2010] <- 0
##Uganda
pol$polity2[pol$ccode==500 & pol$year==1979] <- -7
#Iraq
pol$polity2[pol$ccode==645 & pol$year>=2003 & pol$year<=2009] <- (-9+3)/2
##Syria
pol$polity2[pol$ccode==652 & pol$year>=1958 & pol$year<=1961] <- (7+2)/2
##Lebanon
pol$polity2[pol$ccode==660 & pol$year>=1990 & pol$year<=2004] <- (0+6)/2
##Kuwait
pol$polity2[pol$ccode==690 & pol$year==1990] <- (-10+-9)/2
##Afghanistan
pol$polity2[pol$ccode==700 & pol$year>=1979 & pol$year<=1988] <- (0+-8)/2
pol$polity2[pol$ccode==700 & pol$year>=2002 & pol$year<=2010] <- (-7+-1)/2
##India
pol$polity2[pol$ccode==750 & pol$year>=1947 & pol$year<=1951] <- 5
##Cambodia
pol$polity2[pol$ccode==811 & pol$year>=1979 & pol$year<=1989] <- -7
##South Vietnam
pol$polity2[pol$ccode==817 & pol$year>=1965 & pol$year<=1972] <- (-3+0)/2

d.original <- merge(d.original, pol, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d.original <- merge(d.original, pol, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))

##Code Missing Values (See Discussion in Appendix)
##Kosovo
d.original$polity2A[d.original$statea==341 & d.original$year>=2006 & d.original$year<=2007] <- 8
d.original$polity2B[d.original$stateb==341 & d.original$year>=2006 & d.original$year<=2007] <- 8
#Ghana
d.original$polity2A[d.original$statea==452 & d.original$year>=1957 & d.original$year<=1959] <- 1
d.original$polity2B[d.original$stateb==452 & d.original$year>=1957 & d.original$year<=1959] <- 1
##Zimbabwe/Rhodesia
d.original$polity2A[d.original$statea==552 & d.original$year>=1965 & d.original$year<=1979] <- 4
d.original$polity2B[d.original$stateb==552 & d.original$year>=1965 & d.original$year<=1979] <- 4
##Egypt
d.original$polity2A[d.original$statea==651 & d.original$year>=1946 & d.original$year<=1950] <- (1+5)/2
d.original$polity2B[d.original$stateb==651 & d.original$year>=1946 & d.original$year<=1950] <- (1+5)/2
##Kuwait
d.original$polity2A[d.original$statea==690 & d.original$year>=1961 & d.original$year<=1962] <- -8
d.original$polity2B[d.original$stateb==690 & d.original$year>=1961 & d.original$year<=1962] <- -8
##Afghanistan
d.original$polity2A[d.original$statea==700 & d.original$year==2001] <- -7
d.original$polity2B[d.original$stateb==700 & d.original$year==2001] <- -7
##Bangladesh
d.original$polity2A[d.original$statea==771 & d.original$year==1971] <- 8
d.original$polity2B[d.original$stateb==771 & d.original$year==1971] <- 8
##South Vietnam
d.original$polity2A[d.original$statea==817 & d.original$year==1954] <- -3
d.original$polity2B[d.original$stateb==817 & d.original$year==1954] <- -3

##Identify Regime Transitions
d.original$regtransA[is.na(d.original$regtransA)==T] <- 0
d.original$regtransA[d.original$regtransA==-77] <- 1
d.original$regtransA[d.original$regtransA==-66] <- 1
d.original$regtransA[d.original$regtransA==-2] <- 1
d.original$regtransA[d.original$regtransA==-1] <- 0
d.original$regtransA[d.original$regtransA==1] <- 0
d.original$regtransA[d.original$regtransA==2] <- 0
d.original$regtransA[d.original$regtransA==3] <- 1
d.original$regtransA[d.original$regtransA==96] <- 1
d.original$regtransA[d.original$regtransA==97] <- 1
d.original$regtransA[d.original$regtransA==98] <- 1
d.original$regtransA[d.original$regtransA==99] <- 1

##Create Variables for Major Power Status
major <- read.csv("majors2016.csv", header=T, stringsAsFactors=F)
major

##State A Major Power Status
d.original$majora <- 0
d.original$majora[d.original$statea %in% c(2, 200, 220, 365, 710)] <- 1
d.original$majora[d.original$year >=1991 & d.original$statea %in% c(255, 740)] <- 1

##State B Major Power Status
d.original$majorb <- 0
d.original$majorb[d.original$stateb %in% c(2, 200, 220, 365, 710)] <- 1
d.original$majorb[d.original$year >=1991 & d.original$stateb %in% c(255, 740)] <- 1

##Order Dataframe
d.original <- d.original[order(d.original$year, d.original$statea, d.original$stateb),]

save(d.original, file="analysis_original.RData")

############################################
##Build Country-Year Dataframe #############
############################################

rm(list = ls())
load("nsi_dataset_v1.RData")

##GWF Regime Type
gwf <- read.dta13("GWF_AllPoliticalRegimes.dta")
##Fix Country Codes
##Germany
gwf$cowcode[gwf$cowcode==255 & gwf$year>=1955 & gwf$year <= 1990] <- 255
##Yemen
gwf$cowcode[gwf$cowcode==678 & gwf$year >= 1990] <- 679

gwf <- gwf[gwf$gwf_regimetype!="NA",]
gwf$gwf_regimetype[gwf$gwf_regimetype=="indirect military"] <- "Military"
gwf$gwf_regimetype[gwf$gwf_regimetype=="military-personal"] <- "Mixed"
gwf$gwf_regimetype[gwf$gwf_regimetype=="party-military"] <- "Mixed"
gwf$gwf_regimetype[gwf$gwf_regimetype=="party-military-personal"] <- "Mixed"
gwf$gwf_regimetype[gwf$gwf_regimetype=="party-personal"] <- "Mixed"
gwf$gwf_regimetype[gwf$gwf_regimetype=="monarchy"] <- "Monarchy"
gwf$gwf_regimetype[gwf$gwf_regimetype=="democracy"] <- "Democracy"
gwf$gwf_regimetype[gwf$gwf_regimetype=="military"] <- "Military"
gwf$gwf_regimetype[gwf$gwf_regimetype=="party"] <- "Party"
gwf$gwf_regimetype[gwf$gwf_regimetype=="personal"] <- "Personal"
gwf$gwf_regimetype[gwf$gwf_regimetype=="oligarchy"] <- "Oligarchy"
gwf$gwf_regimetype[gwf$gwf_regimetype=="monarchy"] <- "Monarchy"
var <- c("cowcode", "year", "gwf_regimetype", "gwf_military", "gwf_personal")
gwf <- subset(gwf, select=var)

d.descriptive <- merge(d, gwf, by.x=c("ccode", "year"), by.y=c("cowcode", "year"), all.x = T)

##Fix Missing Values
##Yugoslavia
d.descriptive$gwf_regimetype[d.descriptive$ccode==345 & d.descriptive$year==1991] <- "Mixed"
##India
d.descriptive$gwf_regimetype[d.descriptive$ccode==750 & d.descriptive$year==1947] <- "Democracy"
##Pakistan
d.descriptive$gwf_regimetype[d.descriptive$ccode==770 & d.descriptive$year==1947] <- "Oligarchy"
##Israel
d.descriptive$gwf_regimetype[d.descriptive$ccode==666 & d.descriptive$year==1948] <- "Democracy"
##Vietnam
d.descriptive$gwf_regimetype[d.descriptive$ccode==816 & d.descriptive$year==1954] <- "Party"

##Reset Missing to 0
d.descriptive$gwf_regimetype[is.na(d.descriptive$gwf_regimetype)==T] <- "Democracy"
d.descriptive$gwf_military[is.na(d.descriptive$gwf_military)==T] <- 0
d.descriptive$gwf_personal[is.na(d.descriptive$gwf_personal)==T] <- 0

##CGV Regime Type
cgv <- read.dta13("ddrevisited_data_v1.dta")
cgv <- cgv[is.na(cgv$regime)==F,]
cgv$regime[cgv$regime==0] <- "Parliamentary"
cgv$regime[cgv$regime==1] <- "Semi-Presidential"
cgv$regime[cgv$regime==2] <- "Presidential"
cgv$regime[cgv$regime==3] <- "Dictatorship"
cgv$regime[cgv$regime==4] <- "Dictatorship"
cgv$regime[cgv$regime==5] <- "Dictatorship"
var <- c("cowcode", "year", "regime")
cgv <- subset(cgv, select=var)
d.descriptive <- merge(d.descriptive, cgv, by.x=c("ccode", "year"), by.y=c("cowcode", "year"), all.x = T)

save(d.descriptive, file="figure_1.RData")

############################################
##Build Time Series Crisis Dataframe #######
############################################

rm(list = ls())
system <- read.csv("states2016.csv")
d2 <- read.csv("crisis_dyad.csv", header = T, stringsAsFactors = F)
d3 <- read.csv("crisis_outcomes.csv")

##Create Dataframe for Dyad-Years
system <- system %>%  select(ccode, styear, endyear)
system <- system %>% tidyr::expand(statea=ccode, stateb=ccode, year=seq(1946,2012)) %>%
  filter(statea!=stateb) %>% 
  left_join(., system, by=c("statea"="ccode"), relationship = "many-to-many") %>%
  filter(year >= styear & year <= endyear) %>%
  select(-styear,-endyear) %>% 
  left_join(., system, by=c("stateb"="ccode"), relationship = "many-to-many") %>%
  filter(year >= styear & year <= endyear) %>%
  select(-styear,-endyear)
system <- as.data.frame(system)

##Subset to Trigger Year of Each Crisis-Dyad
d2 <- d2 %>%
  group_by(crisno, statea, stateb) %>%
  arrange(year) %>%
  slice(1) %>%
  ungroup()

##Merge Outcome for Country A to Dyadic Dataframe
d2 <- merge(d2, d3, by.x=c("crisno", "statea"), by.y=c("crisno", "cracid"), all.x=T)

##Merge Outcome for Country B to Dyadic Dataframe
d2 <- merge(d2, d3, by.x=c("crisno", "stateb"), by.y=c("crisno", "cracid"), suffixes=c("A", "B"), all.x=T)

##Create Measures for Crisis and Crisis Failure
d2$crisis <- 1
d2$crisis_initA <- NA
d2$crisis_initA <- ifelse(d2$first_moverA==1,1,0)
d2$crisis_initB <- NA
d2$crisis_initB <- ifelse(d2$first_moverB==1,1,0)
d2$crisis_failA <- NA
d2$crisis_failA <- ifelse((d2$outcomA==3 | d2$outcomA==4),1,0)
d2$crisis_failB <- NA
d2$crisis_failB <- ifelse((d2$outcomB==3 | d2$outcomB==4),1,0)
d2$crisis_init_failA <- NA
d2$crisis_init_failA <- ifelse((d2$first_moverA==1 & (d2$outcomA==3 | d2$outcomA==4)),1,0)
d2$crisis_init_failB <- NA
d2$crisis_init_failB <- ifelse(d2$first_moverB==1 & (d2$outcomB==3 | d2$outcomB==4),1,0)
d2$crisis_successA <- NA
d2$crisis_successA <- ifelse((d2$outcomA==1 | d2$outcomA==2),1,0)
d2$crisis_successB <- NA
d2$crisis_successB <- ifelse((d2$outcomB==1 | d2$outcomB==2),1,0)
d2$crisis_init_successA <- NA
d2$crisis_init_successA <- ifelse((d2$first_moverA==1 & (d2$outcomA==1 | d2$outcomA==2)),1,0)
d2$crisis_init_successB <- NA
d2$crisis_init_successB <- ifelse((d2$first_moverB==1 & (d2$outcomB==1 | d2$outcomB==2)),1,0)

##Create A Holder Dataframe for State A Codings
var <- c("crisno", "year", "statea", "stateb", "crisis", "crisis_initA", "crisis_initB", "crisis_successA", "crisis_successB",
         "crisis_failA", "crisis_failB", "crisis_init_failA", "crisis_init_failB", "crisis_init_successA", "crisis_init_successB")
holder1 <- subset(d2, select=var)

##Create A Second Holder Dataframe for State B Codings
holder2 <- holder1
colnames(holder2) <- c("crisno", "year", "stateb", "statea", "crisis", "crisis_initB", "crisis_initA", "crisis_successB", "crisis_successA",
                       "crisis_failB", "crisis_failA", "crisis_init_failB", "crisis_init_failA", "crisis_init_successB", "crisis_init_successA")

##Merge Holder Dataframes
master <- rbind(holder1, holder2)
master <- master[order(master$crisno, master$year, master$statea, master$stateb),]

##Manually Fix Erroneous Country Code for Yemen
master$statea[master$statea==678 & master$year>=1993 & is.na(master$stateb)==F] <- 679
master$stateb[master$stateb==678 & master$year>=1993 & is.na(master$stateb)==F] <- 679

##Limit to Study Period (1946 to 2012)
master <- master[master$year>=1946 & master$year<=2012 & is.na(master$year)==F,]

##Merge into Dyad-Year Dataframe
tseries <- merge(system, master, by.x=c("year","statea", "stateb"), by.y=c("year", "statea", "stateb"), all.x=T)
tseries <- tseries[order(tseries$year, tseries$statea, tseries$stateb),]

##Create 0 Values
tseries$crisis[is.na(tseries$crisis)==T] <- 0
table(tseries$crisis)

tseries$crisis_initA[is.na(tseries$crisis_initA)==T] <- 0
tseries$crisis_initB[is.na(tseries$crisis_initB)==T] <- 0

tseries$crisis_successA[is.na(tseries$crisis_successA)==T] <- 0
tseries$crisis_successB[is.na(tseries$crisis_successB)==T] <- 0

tseries$crisis_failA[is.na(tseries$crisis_failA)==T] <- 0
tseries$crisis_failB[is.na(tseries$crisis_failB)==T] <- 0

tseries$crisis_init_failA[is.na(tseries$crisis_init_failA)==T] <- 0
tseries$crisis_init_failB[is.na(tseries$crisis_init_failB)==T] <- 0

tseries$crisis_init_successA[is.na(tseries$crisis_init_successA)==T] <- 0
tseries$crisis_init_successB[is.na(tseries$crisis_init_successB)==T] <- 0

##Subset to Politically Relevant Dyads (Major Powers and Contiguous States)

##State A Major Power Status
tseries$majora <- 0
tseries$majora[tseries$statea %in% c(2, 200, 220, 365, 710)] <- 1
tseries$majora[tseries$year >=1991 & tseries$statea %in% c(255, 740)] <- 1

##State B Major Power Status
tseries$majorb <- 0
tseries$majorb[tseries$stateb %in% c(2, 200, 220, 365, 710)] <- 1
tseries$majorb[tseries$year >=1991 & tseries$stateb %in% c(255, 740)] <- 1

##Geographic Continuity
cont <- read.csv("contdird.csv", header=T, stringsAsFactors=F)
cont <- subset(cont, select=c("state1no", "state2no", "conttype", "year"))
tseries <- merge (tseries, cont, by.x=c("statea", "stateb", "year"), by.y=c("state1no", "state2no", "year"), all.x=T)
tseries$conttype[is.na(tseries$conttype)] <- 0

##Create dummy variable for continuity
tseries$contig <- 0
tseries$contig[tseries$conttype==1 | tseries$conttype==2 | tseries$conttype==3] <- 1

##Politically Relevant Dyads
tseries <- tseries[tseries$contig==1 | tseries$majora==1 | tseries$majorb==1,]

##Create Variables for Time Since Last Crisis
tseries$dirdyadid <- paste(tseries$statea, tseries$stateb, sep="-")
tseries <- tseries[order(tseries$dirdyadid, tseries$year),]
tseries$time <- sequence(rle(paste(tseries$dirdyadid, tseries$crisisA))$lengths)
imax <- nrow(tseries)
pb <- txtProgressBar(min = 0, max = imax, style = 3)
for(i in c(1:nrow(tseries))){
  if(tseries$year[i]==1946){
    tseries$time[i] <- 0
  } else if(tseries$dirdyadid[i]!=tseries$dirdyadid[i-1]){
    tseries$time[i] <- 0
  } else if(tseries$crisis[i-1]==1 & tseries$dirdyadid[i]==tseries$dirdyadid[i-1]){
    tseries$time[i] <- 0
  } else tseries$time[i] <- 1 + tseries$time[i-1]
  setTxtProgressBar(pb, i)
}
tseries$time <- tseries$time/100
tseries$time2 <- tseries$time^2
tseries$time3 <- tseries$time^3

############################################
##Merge Dataframe with NSI Data ############
############################################

load("nsi_dataset_v1.RData")
d <- merge(tseries, d, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x = T, all.y = F)

############################################
##Merge Covariate Data #####################
############################################

##National Military Capabilities
nmc <- read.dta13("NMC_5_0.dta")
var <- c("ccode", "year", "cinc")
nmc <- subset(nmc, select = var)
d <- merge(d, nmc, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d <- merge(d, nmc, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))
d$cinc_rel <- d$cincA / (d$cincA + d$cincB)
d$LcincA <- log(d$cincA+1)
d$LcincB <- log(d$cincB+1)
d$Lcinc_rel <- d$LcincA / (d$LcincA + d$LcincB)

##ATOP Alliances S Scores
alliance <- read.csv("atop_sscores.csv", header=T, stringsAsFactors=F)
alliance.ab <- alliance[alliance$year>=1946,]
var <- c("ccode1", "ccode2", "year", "s_un_atop", "s_wt_atop")
alliance.ab <- subset(alliance.ab, select=var)
alliance.ba <- alliance.ab
colnames(alliance.ba) <- c("ccode2", "ccode1", "year", "s_un_atop", "s_wt_atop")
alliance <- rbind(alliance.ab, alliance.ba)
d <- merge(d, alliance, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GDP
gdp <- read.delim("gdpv6.txt", header = TRUE, sep = "\t", dec = ".")
gdp$Lrealgdp <- log(gdp$realgdp+1)
var <- c("statenum", "year", "Lrealgdp")
gdp <- subset(gdp, select=var)
d <- merge(d, gdp, by.x=c("statea", "year"), by.y=c("statenum", "year"), all.x=T)
d <- merge(d, gdp, by.x=c("stateb", "year"), by.y=c("statenum", "year"), all.x=T, suffixes = c("A", "B"))

##Trade
trade <- read.csv("dyad_trade_cow_v4.csv", header=T, stringsAsFactors=F, na.strings = -9)
trade$trade <- trade$flow1 + trade$flow2
trade$Ltrade <- log(trade$trade+1)
var <- c("ccode1", "ccode2", "year", "Ltrade")
trade.ab <- subset(trade, select=var)
trade.ba <- trade.ab
colnames(trade.ab) <- c("ccode2", "ccode1", "year", "Ltrade")
trade <- rbind(trade.ab, trade.ba)
d <- merge(d, trade, by.x=c("statea","stateb","year"), by.y=c("ccode1","ccode2","year"), all.x=T)

##GWF Regime Type
gwf <- read.dta13("GWF_AllPoliticalRegimes.dta")
##Standardize Country Codes
##Germany
gwf$cowcode[gwf$cowcode==255 & gwf$year>=1955 & gwf$year <= 1990] <- 260
##Yemen
gwf$cowcode[gwf$cowcode==678 & gwf$year >= 1990] <- 679

var <- c("cowcode", "year", "gwf_military", "gwf_personal")
gwf <- subset(gwf, select=var)
d <- merge(d, gwf, by.x=c("statea", "year"), by.y=c("cowcode", "year"), all.x = T)

##Manually Code Missing Values (See Discussion in Appendix)
##Yugoslavia
d$gwf_personal[d$statea==345 & d$year==1991] <- 1
d$gwf_military[d$statea==345 & d$year==1991] <- 0
##Bosnia
d$gwf_personal[d$statea==346 & d$year==2010] <- 0
d$gwf_military[d$statea==346 & d$year==1991] <- 0
d$gwf_military[d$statea==346 & d$year==2010] <- 0
##India
d$gwf_military[d$statea==750 & d$year==1947] <- 0
d$gwf_personal[d$statea==750 & d$year==1947] <- 0
##Pakistan
d$gwf_military[d$statea==770 & d$year==1947] <- 0
d$gwf_personal[d$statea==770 & d$year==1947] <- 0
##Israel
d$gwf_military[d$statea==666 & d$year==1948] <- 0
d$gwf_personal[d$statea==666 & d$year==1948] <- 0
##Vietnam
d$gwf_military[d$statea==816 & d$year==1954] <- 0
d$gwf_personal[d$statea==816 & d$year==1954] <- 0

##Merge Leaders Data
leaders <- read.csv("leaders_updated.csv", stringsAsFactors = F, header = T)
leaders <- subset(leaders, select=c("leader", "ccode", "year", "yrborn", "gender", "milservice", "combat", "hos_experience"))
leaders <- leaders %>%
  filter(year <= 2012 & year >=1946) %>% 
  group_by(ccode, year) %>%
  arrange(year) %>%
  slice(1) %>%
  ungroup()
d <- merge(d, leaders, by.x = c("statea", "year", "hos_name_arc"), by.y = c("ccode", "year", "leader"), all.x = T, all.y = F)

##Create Variable for Leader Age
d$age <- d$year - d$yrborn

##Polity V
pol <- read.csv("p5v2018.csv", stringsAsFactors = F, header = T)
var <- c("ccode", "year", "democ", "autoc", "polity2", "regtrans")
pol <- subset(pol, select = var)

##Standardize Polity Country Codes
##Soviet Union
pol$ccode[pol$ccode==364] <- 365
##Ethiopia
pol$ccode[pol$ccode==529] <- 530
##Pakistan
pol$ccode[pol$ccode==769] <- 770
##Vietnam
pol$ccode[pol$ccode==818] <- 816
##Yugoslavia/Serbia
pol$ccode[pol$ccode==347] <- 345
pol$ccode[pol$ccode==342] <- 345
##Drop Duplicates
pol <- pol %>% distinct(ccode, year, .keep_all = TRUE)

##Code Transition Cases (See Discussion in Appendix)
##East Germany
pol$polity2[pol$ccode==265 & pol$year>=1989 & pol$year<=1990] <- (-9+-9)/2
##Hungary
pol$polity2[pol$ccode==310 & pol$year==1956] <- (-7+-7)/2
##Czechoslovakia
pol$polity2[pol$ccode==315 & pol$year==1968] <- (-7+-7)/2
##Bosnia
pol$polity2[pol$ccode==346 & pol$year>=1995 & pol$year<=2010] <- 0
##Uganda
pol$polity2[pol$ccode==500 & pol$year==1979] <- -7
#Iraq
pol$polity2[pol$ccode==645 & pol$year>=2003 & pol$year<=2009] <- (-9+3)/2
##Syria
pol$polity2[pol$ccode==652 & pol$year>=1958 & pol$year<=1961] <- (7+2)/2
##Lebanon
pol$polity2[pol$ccode==660 & pol$year>=1990 & pol$year<=2004] <- (0+6)/2
##Kuwait
pol$polity2[pol$ccode==690 & pol$year==1990] <- (-10+-9)/2
##Afghanistan
pol$polity2[pol$ccode==700 & pol$year>=1979 & pol$year<=1988] <- (0+-8)/2
pol$polity2[pol$ccode==700 & pol$year>=2002 & pol$year<=2010] <- (-7+-1)/2
##India
pol$polity2[pol$ccode==750 & pol$year>=1947 & pol$year<=1951] <- 5
##Cambodia
pol$polity2[pol$ccode==811 & pol$year>=1979 & pol$year<=1989] <- -7
##South Vietnam
pol$polity2[pol$ccode==817 & pol$year>=1965 & pol$year<=1972] <- (-3+0)/2

d <- merge(d, pol, by.x=c("statea", "year"), by.y=c("ccode", "year"), all.x=T)
d <- merge(d, pol, by.x=c("stateb", "year"), by.y=c("ccode", "year"), all.x=T, suffixes=c("A","B"))

##Code Missing Values (See Discussion in Appendix)
##Kosovo
d$polity2A[d$statea==341 & d$year>=2006 & d$year<=2007] <- 8
d$polity2B[d$stateb==341 & d$year>=2006 & d$year<=2007] <- 8
#Ghana
d$polity2A[d$statea==452 & d$year>=1957 & d$year<=1959] <- 1
d$polity2B[d$stateb==452 & d$year>=1957 & d$year<=1959] <- 1
##Zimbabwe/Rhodesia
d$polity2A[d$statea==552 & d$year>=1965 & d$year<=1979] <- 4
d$polity2B[d$stateb==552 & d$year>=1965 & d$year<=1979] <- 4
##Egypt
d$polity2A[d$statea==651 & d$year>=1946 & d$year<=1950] <- (1+5)/2
d$polity2B[d$stateb==651 & d$year>=1946 & d$year<=1950] <- (1+5)/2
##Kuwait
d$polity2A[d$statea==690 & d$year>=1961 & d$year<=1962] <- -8
d$polity2B[d$stateb==690 & d$year>=1961 & d$year<=1962] <- -8
##Afghanistan
d$polity2A[d$statea==700 & d$year==2001] <- -7
d$polity2B[d$stateb==700 & d$year==2001] <- -7
##Bangladesh
d$polity2A[d$statea==771 & d$year==1971] <- 8
d$polity2B[d$stateb==771 & d$year==1971] <- 8
##South Vietnam
d$polity2A[d$statea==817 & d$year==1954] <- -3
d$polity2B[d$stateb==817 & d$year==1954] <- -3

##Identify Regime Transitions
d$regtransA[is.na(d$regtransA)==T] <- 0
d$regtransA[d$regtransA==-77] <- 1
d$regtransA[d$regtransA==-66] <- 1
d$regtransA[d$regtransA==-2] <- 1
d$regtransA[d$regtransA==-1] <- 0
d$regtransA[d$regtransA==1] <- 0
d$regtransA[d$regtransA==2] <- 0
d$regtransA[d$regtransA==3] <- 1
d$regtransA[d$regtransA==96] <- 1
d$regtransA[d$regtransA==97] <- 1
d$regtransA[d$regtransA==98] <- 1
d$regtransA[d$regtransA==99] <- 1

##Order Dataframe
d <- d[order(d$year, d$statea, d$stateb),]

save(d, file="tseries.RData")
